<?php
namespace Unicom\Controller;
use Think\Controller;

class CtccController extends AdminController {

	public function list_() {
		layout(false);
		$model = M('wa.verify', 'ctcc_');
		$REQUEST = (array) I('request.');
		$product_type = I('param.product_type', 0);

		$search = $REQUEST["search"];
		import('ORG.Util.Page'); // 导入分页类
		if (!empty($search)) {
			$where = "where link_id like '$search%' or phone like '$search%' or channel=$search";
		}

		$count = $model->query("select count(*) as total_count from (select FROM_UNIXTIME(create_at, '%Y-%m-%d') as today,create_at,
		count(*) as up_count
		from wa.ctcc_verify_phone where product_type=$product_type
		group by today,channel) as b")[0]['total_count']; // 查询满足要求的总记录数

		$listRows = 10;
		if (isset($REQUEST['r'])) {
			$listRows = (int) $REQUEST['r'];
		} else {
			$listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
		}
		$Page = new \Think\Page($count, $listRows); // 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show(); // 分页显示输出

		//$data = $model->query("call wa.cuccList(" . $Page->firstRow . "," . $listRows . ")");

		$sql = "select FROM_UNIXTIME(create_at, '%Y-%m-%d') as today,create_at,channel from wa.ctcc_verify_phone where product_type=$product_type  group by today,channel order by today desc limit " . $Page->firstRow . "," . $listRows;

		$data = $model->query($sql);

		foreach ($data as $key => &$value) {
			$today = $value["today"];
			$channel = $value["channel"];
			$upcount = $model->query("select count(*) as up_count from wa.ctcc_verify_phone where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and channel=$channel")[0]['up_count'];
			$value["up_count"] = $upcount;
			$value["success_count"] = $model->query("select count(*) as success_count from wa.ctcc_buy_product where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and is_sub_product=1 and channel=$channel")[0]["success_count"];
			$value["sync_count"] = $model->query("select count(*) as sync_count from wa.ctcc_buy_product where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and sync=1 and channel=$channel")[0]["sync_count"];
			$value["token_count"] = $model->query("select count(*) as token_count from wa.ctcc_buy_product where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and channel=$channel")[0]["token_count"];
		}
		//var_dump($data);
		$this->assign('data', $data);
		$this->assign('product_id', $product_id);
		$this->assign('page', $show);
		//	var_dump($Page, $count);
		$this->display();

	}

	public function find_() {
		layout(false);
		$model = M('wa.verify', 'ctcc_');
		$product_type = I('param.product_type', 0);
		if (IS_POST) {
			$phone = I('param.phone', '');

			$data = $model->query("select a.*,b.dcprivence,b.dccity,
				FROM_UNIXTIME(a.create_at, '%Y-%m-%d %h:%i:%s') as create_at1


			from wa.ctcc_buy_product as a
			left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile
			

			where a.phone=$phone and a.product_type=$product_type order by a.create_at desc ");
			$this->assign('product_type', $product_type);
			$this->assign('data', $data);

			$this->display();
		} else {
			$this->assign('product_type', $product_type);
			$this->display();
		}

	}

	//查询用户订购信息
	public function get_subproducts($number) {
		layout(false);

		$str = file_get_contents("http://wawa.fm:8088/ctcc/v1/api/queryUserInfo?number=" . $number);

		$data = object_array(json_decode($str));
		$this->assign('data', $data);
		$this->assign('number', $number);

		$this->display();

	}
	//查询用户音乐盒信息
	public function get_subbox($token) {
		layout(false);

		$str = file_get_contents("http://wawa.fm:8088/cucc/v1/api/qrySubedBox?token=" . $token);

		$data = object_array(json_decode($str));
		$this->assign('data', $data);
		$this->assign('token', $token);

		$this->display();

	}

	//退订产品
	public function unSubProduct($mdn) {
		//layout(false);
		$url = "http://wawa.fm:8088/ctcc/v1/api/unsubscribebyemp?mdn=" . $mdn;
		$str = file_get_contents($url);


		$this->success($str);

	}

	//上行详情

	public function upinfo_() {
		layout(false);
		$model = M('wa.verify', 'ctcc_');
		$today = I('param.today', '');
		$product_id = I('param.product_id', '');
		$channel = I('param.channel', '');
		$data = $model->query("select a.*,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at1 ,b.dcprivence,b.dccity,
		IF(status,'成功','成功') as tips
		from wa.ctcc_verify_phone as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile
		 where FROM_UNIXTIME(a.create_at, '%Y-%m-%d')='$today' and a.channel='$channel' order by a.create_at desc ");

		$this->assign('data', $data);
		$this->assign("export", U('ring/export', array('today' => $today, 'channel' => $channel, 'tag' => 1)));
		$this->display();

	}
	public function tokeninfo_() {
		layout(false);
		$model = M('wa.verify', 'ctcc_');
		$today = I('param.today', '');
		$product_id = I('param.product_id', '');
		$channel = I('param.channel', '');
		$data = $model->query("select a.*,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at1 ,b.dcprivence,b.dccity,FROM_UNIXTIME(create_at, '%Y-%m-%d') as today,
		IF(is_sub_product,'成功','失败') as tips
		from wa.ctcc_buy_product as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile where FROM_UNIXTIME(a.create_at, '%Y-%m-%d')='$today' and a.channel=$channel order by a.create_at desc");

		$this->assign('data', $data);
		$this->assign("export", U('ring/export', array('today' => $today, 'channel' => $channel, 'tag' => 2)));

		$this->display();

	}

	public function successinfo_() {
		layout(false);
		$model = M('wa.verify', 'ctcc_');
		$today = I('param.today', '');
		$product_id = I('param.product_id', '');
		$channel = I('param.channel', '');
		$data = $model->query("select a.*,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at1 ,b.dcprivence,b.dccity,

		IF(is_sub_product,'成功','失败') as tips,


		IF(is_buy_vip_tone,'下载成功','下载失败') as tone_tips
		from wa.ctcc_buy_product as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile
		
		 where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and a.channel='$channel' and a.is_sub_product=1 order by a.create_at desc");

		$data2 = $model->query("select b.dcprivence,count(*) as total_count
		from wa.ctcc_buy_product as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile
		where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and a.channel='$channel' and a.is_sub_product=1  group by dcprivence order by a.create_at desc");

		$this->assign('data', $data);
		$this->assign('data1', $data2);
		$this->assign("export", U('ring/export', array('today' => $today, 'channel' => $channel, 'tag' => 3)));

		$this->display();

	}

	//导出excel
	public function export($tag) {
		layout(false);
		$model = M('wa.verify', 'ctcc_');
		$today = I('param.today', '');

		$channel = I('param.channel', '');
		switch ($tag) {
		case '1':
			$data = $model->query("select a.link_id,a.phone,a.channel,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at1 ,b.dcprivence,b.dccity,
		IF(status,'成功','失败') as tips
		from wa.ctcc_verify_phone as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile
		 where FROM_UNIXTIME(a.create_at, '%Y-%m-%d')='$today' and a.channel='$channel' order by a.create_at desc ");
			$title = array('link_id', 'phone', 'channel', 'create_at1', 'dcprivence', 'dccity', 'tips', 'message');

			exportexcel($data, $title, $today . '_上行');

			break;
		case '2':

			$data = $model->query("select a.link_id,a.phone,a.channel,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at1 ,b.dcprivence,b.dccity,
		IF(is_sub_product,'成功','失败') as tips,a.message
		from wa.ctcc_buy_product as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile where FROM_UNIXTIME(a.create_at, '%Y-%m-%d')='$today' and a.channel=$channel order by a.create_at desc");
			$title = array('link_id', 'phone', 'channel', 'create_at1', 'dcprivence', 'dccity', 'tips', 'message');

			exportexcel($data, $title, $today . '_token');
			break;
		case '3':
			$data = $model->query("select a.link_id,a.phone,a.channel,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at1 ,b.dcprivence,b.dccity,

		IF(is_sub_product,'成功','失败') as tips,
		


		IF(is_buy_vip_tone,'下载成功','下载失败') as tone_tips
		from wa.ctcc_buy_product as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile
		
		 where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and a.channel='$channel' and a.is_sub_product=1 order by a.create_at desc");
			$title = array('link_id', 'phone', 'channel', 'create_at1', 'dcprivence', 'dccity', 'tips', 'songname', 'tone_tips');

			exportexcel($data, $title, $today . '_成功');

			break;
		case '4':
			$data = $model->query("select a.link_id,a.phone,a.channel,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at1 ,b.dcprivence,b.dccity,
		IF(is_sub_product,'成功','失败') as tips,IF(is_buy_vip_tone,'下载成功','下载失败') as tone_tips
		from wa.ctcc_buy_product as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile
	
		 where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and a.channel='$channel' and a.sync=1 order by a.create_at desc");
			$title = array('link_id', 'phone', 'channel', 'create_at1', 'dcprivence', 'dccity', 'tips', 'tone_tips', 'songname');

			exportexcel($data, $title, $today . '_同步成功');

			break;

		default:
			# code...
			break;
		}

	}

	public function syncinfo_() {
		layout(false);
		$model = M('wa.verify', 'ctcc_');
		$today = I('param.today', '');
		$product_id = I('param.product_id', '');
		$channel = I('param.channel', '');
		$data = $model->query("select a.*,FROM_UNIXTIME(create_at, '%Y-%m-%d %h:%i:%s') as create_at1 ,b.dcprivence,b.dccity,
		IF(is_sub_product,'成功','失败') as tips,IF(is_buy_vip_tone,'下载成功','下载失败') as tone_tips
		from wa.ctcc_buy_product as a
		left join cp.cp_privence as b on left (a.phone,7)=b.dcmobile
		
		 where FROM_UNIXTIME(create_at, '%Y-%m-%d')='$today' and a.channel='$channel' and a.sync=1 order by a.create_at desc");

		$this->assign('data', $data);
		$this->assign("export", U('ring/export', array('today' => $today, 'channel' => $channel, 'tag' => 4)));

		$this->display();

	}

}